/**
 * 清除所有 cookie
 */
function clearAllCookie() {
  var date = new Date()
  date.setTime(date.getTime() - 10000)
  var keys = document.cookie.match(/[^ =;]+(?=\=)/g)
  if (keys) {
    for (var i = keys.length; i--; ) {
      document.cookie = keys[i] + '=; expire=' + date.toGMTString() + '; path=/'
    }
  }
}

/**
 * 设置 cookie
 * @param {string} key 键
 * @param {string} value 值
 * @param {data} time 过期时间(秒)
 */
function setCookie(key, value, time) {
  var oDate = new Date()
  oDate.setDate(Date.now() + time)
  document.cookie = key + '=' + value + '; expires=' + oDate.toDateString()
}

/**
 * 获取 cookie
 * @param {string} name
 */
function getCookie(name) {
  var strcookie = document.cookie // 获取cookie字符串
  var arrcookie = strcookie.split('; ') // 分割
  for (var i = 0; i < arrcookie.length; i++) {
    var arr = arrcookie[i].split('=')
    if (arr[0] == name) {
      return arr[1]
    }
  }
  return ''
}

module.exports = {
  setCookie, // 设置cookie
  getCookie, // 获取cookie
  clearAllCookie // 清除所有cookie
}
